home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / 68k Internet Config ƒ / C Source ƒ / IC Text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-02  |  5.6 KB  |  262 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC Text.c
  3.     
  4.     
  5.     The pascal code uses a routine, NopCaretHook, to handle locked text resources.  Basically
  6.     it is a specialized caret drawing procedure that doesn't draw any caret (making the text
  7.     item look like it is read-only).
  8.     
  9.     For the 68k code, I could have just implemented the routine in a function and make the body
  10.     consist of asm statements.  This would not work for the PPC version, however.
  11.     
  12.     And since you can't write a caret proc in a high level language (see TextEdit.h to find out
  13.     why), I needed to implement the routine in assembler.  Since Sym C++ 8.0 doesn't come
  14.     with an assembler (version 8.0.4 is supposed to have one, but I haven't received my copy
  15.     yet), that means that the only way to do it is with a 68k assembly routine.
  16.     
  17.     So I generated a small resource, 'NopC' id 128, which contains the code that would have been
  18.     compiled from the original code.  This resource is read in, load and locked high, and used
  19.     whenever the hidden caret needs to be used.
  20.     
  21.     Note that the only reason I am not implementing this in the 68k code and using the resource
  22.     for PPC code is because I don't like to have to maintain separate pieces of the code for each
  23.     processor type.
  24. */
  25.  
  26. #include <Scrap.h>
  27.  
  28. #include "IC Text.h"
  29. #include "IC Dialogs.h"
  30.  
  31. Handle __NopCaretHook=(Handle)0;
  32.  
  33. OSErr TextCreate(Ptr* data,DialogPtr window,short item,short font,short size,Boolean locked){
  34.     OSErr err;
  35.     ItemDataPtr idp;
  36.     Rect view,dest;
  37.     SavedWindowInfo saved;
  38.     Rect r;
  39.     long wflags;
  40.     Style face=0; // normal
  41.     
  42.     *data=(Ptr)0;
  43.     
  44.     idp=(ItemDataPtr)NewPtr(sizeof(ItemData));
  45.     err=MemError();
  46.     
  47.     if (err==noErr){
  48.         *data=(Ptr)idp;
  49.         idp->window=window;
  50.         idp->item=item;
  51.         idp->te=(TEHandle)0;
  52.         idp->active=true;
  53.         
  54.         EnterWindow(window,font,size,face,&saved);
  55.         
  56.         // get the normal rectangle
  57.         GetDItemRect(window,item,&r);
  58.         
  59.         // convert to the long rectangle used by Waste
  60.         dest=r;
  61.         view=r;
  62.         
  63.         GetFontInfo(&(idp->fi));
  64.         
  65.         idp->lineheight=idp->fi.leading+idp->fi.ascent+idp->fi.descent;
  66.         
  67.         // round to exact line height
  68.         dest.bottom=dest.top + ((dest.bottom-dest.top)/(idp->lineheight))*idp->lineheight;
  69.         
  70.         idp->te=TENew(&dest,&view);
  71.         
  72.         if (locked){
  73.             // disable the caret
  74.             
  75.             if (__NopCaretHook==(Handle)0){
  76.                 // must load the caret hook proc
  77.                 
  78.                 // this is a 68k routine, so there is nothing really to do with it once it
  79.                 // is loaded
  80.                 
  81.                 __NopCaretHook=GetResource('NopC',128);
  82.                 err=ResError();
  83.                 
  84.                 if (err==noErr){
  85.                     // detach it so we keep it around
  86.                     DetachResource(__NopCaretHook);
  87.                     
  88.                     // move it high and lock it down
  89.                     HLockHi(__NopCaretHook);
  90.                 }
  91.             }
  92.             
  93.             if (err==noErr){
  94.                 // no errors loading resource, set the caret hook proc
  95.                 
  96.                 /*
  97.                     We can do the following (set a proc ptr without building a routine
  98.                     descriptor) because we know the code is 68k; the MixedModeMgr
  99.                     will handle it correctly...
  100.                 */
  101.                 (*(idp->te))->caretHook=(CaretHookUPP)(*__NopCaretHook);
  102.             }
  103.         }
  104.         
  105.         TEAutoView(true,idp->te);
  106.         
  107.         ExitWindow(&saved);
  108.         
  109.         if (err!=noErr)
  110.             TextDestroy(data);
  111.     }
  112.     
  113.     return err;
  114. }
  115.  
  116. void TextDestroy(Ptr* data){
  117.     ItemDataPtr idp=(ItemDataPtr)(*data);
  118.     
  119.     if (*data!=(Ptr)0){
  120.         if (idp->te!=(TEHandle)0)
  121.             TEDispose(idp->te);
  122.         DisposePtr(*data);
  123.         *data=0;
  124.     }
  125. }
  126.  
  127. void TextDraw(Ptr data){
  128.     ItemDataPtr idp=(ItemDataPtr)data;
  129.     Rect r;
  130.     
  131.     GetDItemRect(idp->window,idp->item,&r);
  132.     EraseRect(&r);
  133.     TEUpdate(&(*(idp->te))->viewRect,idp->te);
  134. }
  135.  
  136. void TextActivate(Ptr data,Boolean activate){
  137.     ItemDataPtr idp=(ItemDataPtr)(data);
  138.     
  139.     idp->active=activate;
  140.     if (activate)
  141.         TEActivate(idp->te);
  142.     else
  143.         TEDeactivate(idp->te);
  144. }
  145.  
  146. void TextClick(Ptr data,EventRecord* er){
  147.     ItemDataPtr idp=(ItemDataPtr)(data);
  148.     ControlHandle control;
  149.     short part;
  150.     
  151.     SetPort(idp->window);
  152.     GlobalToLocal(&(er->where));
  153.     part=FindControl(er->where,idp->window,&control);
  154.     if (part==0){
  155.         if (PtInRect(er->where,&(*(idp->te))->viewRect)){
  156.             TEClick(er->where,(er->modifiers & shiftKey)!=0,idp->te);
  157.         }
  158.     }
  159. }
  160.  
  161. void TextIdle(Ptr data){
  162.     ItemDataPtr idp=(ItemDataPtr)(data);
  163.     
  164.     TEIdle(idp->te);
  165. }
  166.  
  167. void TextKey(Ptr data,EventRecord* er){
  168.     ItemDataPtr idp=(ItemDataPtr)(data);
  169.     
  170.     if ((er->modifiers & cmdKey)==0){
  171.         TEKey((er->message & 0x00ff),idp->te);
  172.     }
  173. }
  174.  
  175. void TextSetSelect(Ptr data,long selStart,long selEnd){
  176.     ItemDataPtr idp=(ItemDataPtr)(data);
  177.     
  178.     TESetSelect(selStart,selEnd,idp->te);
  179. }
  180.  
  181. void TextGetSelect(Ptr data,long* selStart,long* selEnd){
  182.     ItemDataPtr idp=(ItemDataPtr)(data);
  183.     
  184.     *selStart=(*(idp->te))->selStart;
  185.     *selEnd=(*(idp->te))->selEnd;
  186. }
  187.  
  188. void TextGetSize(Ptr data,long* text_size){
  189.     ItemDataPtr idp=(ItemDataPtr)(data);
  190.     
  191.     *text_size=GetHandleSize((*(idp->te))->hText);
  192. }
  193.  
  194. void TextInsert(Ptr data,Handle h){
  195.     ItemDataPtr idp=(ItemDataPtr)(data);
  196.     SignedByte s;
  197.     OSErr err;
  198.     short fea;
  199.     
  200.     s=HGetState(h);
  201.     HLock(h);
  202.     TEInsert(*h,GetHandleSize(h),idp->te);
  203.     HSetState(h,s);
  204. }
  205.  
  206. void TextGet(Ptr data,Handle h){
  207.     ItemDataPtr idp=(ItemDataPtr)(data);
  208.     long source_size;
  209.     Handle source;
  210.     
  211.     source=(Handle)TEGetText(idp->te);
  212.     source_size=GetHandleSize(source);
  213.     SetHandleSize(h,source_size);
  214.     if (MemError()==noErr){
  215.         BlockMoveData(*source,*h,source_size);
  216.     } else 
  217.         SetHandleSize(h,0);
  218. }
  219.  
  220. void TextMove(Ptr data,Rect* r){
  221.     ItemDataPtr idp=(ItemDataPtr)(data);
  222.     
  223.     (*(idp->te))->viewRect=*r;
  224.     (*(idp->te))->destRect=*r;
  225.     
  226.     TECalText(idp->te);
  227. }
  228.  
  229. void TextCut(Ptr data){
  230.     ItemDataPtr idp=(ItemDataPtr)(data);
  231.     
  232.     TECut(idp->te);
  233.     ZeroScrap();
  234.     TEToScrap();
  235. }
  236.  
  237. void TextCopy(Ptr data){
  238.     ItemDataPtr idp=(ItemDataPtr)(data);
  239.     
  240.     TECopy(idp->te);
  241.     ZeroScrap();
  242.     TEToScrap();
  243. }
  244.  
  245. void TextPaste(Ptr data){
  246.     ItemDataPtr idp=(ItemDataPtr)(data);
  247.     
  248.     if (TEFromScrap()==noErr)
  249.         TEPaste(idp->te);
  250. }
  251.  
  252. void TextClear(Ptr data){
  253.     ItemDataPtr idp=(ItemDataPtr)(data);
  254.     
  255.     TEDelete(idp->te);
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.